﻿using System.Linq;

namespace Basmus.Values
{
    public class SetEqualCondition : ICondition
    {
        public bool Result { get; private set; }

        public SetEqualType SetEqualType { get; private set; }

        public object[] Values { get; private set; }

        public SetEqualCondition(SetEqualType setEqualType, bool result, object[] values)
        {
            SetEqualType = setEqualType;
            Result = result;
            Values = values;
        }

        public bool Check(object[] values)
        {
            if (values!=null && Values!=null)
            {
                if (values.Length > 0&&Values.Length>0)
                {
                    if (values.Length==Values.Length)
                    {
                        var equal = (SetEqualType == SetEqualType.SetEqual && !values.Except(Values).Any()) ||
                                    (SetEqualType == SetEqualType.OrderSetEqual && values.SequenceEqual(Values));
                        return equal == Result;
                    }
                    return Result == false;
                }
                return Result == (values.Length == 0 && Values.Length == 0);
            }
            return Result == (values == null && Values == null);
        }
    }
}